package com.itheima.controller;

import com.itheima.POJO.CheckGroup;
import com.itheima.POJO.Setmeal;
import com.itheima.VO.setmeal_ids;
import com.itheima.constant.MessageConstant;
import com.itheima.constant.RedisConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;

import com.itheima.service.setmealService;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.utils.QiniuUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import redis.clients.jedis.JedisPool;

import java.io.IOException;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/setmeal")
public class setmealController {

    @Reference
    private setmealService setmealService;

    @Autowired
    private JedisPool jedisPool;

    //查询所有检查套餐信息
    @RequestMapping("/findAllSetmeal")
    public Result findAllSetmeal() {
        try {
            List<Setmeal> setmeals = setmealService.findAllSetmeal();
            return new Result(true,MessageConstant.GET_SETMEAL_LIST_SUCCESS,setmeals);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.GET_SETMEAL_LIST_FAIL);
        }
    }

    @PreAuthorize("hasAuthority('SETMEAL_ADD')")
    @RequestMapping("/findAllCheckgroup")
    public Result findAllcheckgroup() {
        List<CheckGroup> allCheckgroup = setmealService.findAllCheckgroup();
        if (allCheckgroup!=null && allCheckgroup.size()>0){
            return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS,allCheckgroup);
        }
        return new Result(false,"获取检查组失败或者没有检测到检查组!");
    }

    @PreAuthorize("hasAuthority('SETMEAL_ADD')")
    @RequestMapping("/upload")
    public Result upload(@RequestParam ("imgFile") MultipartFile imgFile){
        //前段传过来的 参数名称 是否为imgFile 不是的话强制为imgFile

        //此时的imgFile实际上是 该文件的对象,该文件信息在此对象中
        try {
            //得到文件原来的名称
            String originalFilename = imgFile.getOriginalFilename();
            //不用原本的文件名,用后面随机生成的字符串作为文件名,而需要自行补充一个后缀
            //从而我们应该自己得到原文件的后缀名
            //为了找到最后的后缀名(带.的) 拿到最后一个点的位置 进行截取
            int index = originalFilename.lastIndexOf(".");
            //截取到文件的后缀名
            String suffix = originalFilename.substring(index);
            //用UUID得到个随机的唯一的字符串 把它加上后缀  作为保存的图片名
            String fileName = UUID.randomUUID().toString() + suffix;
            //用七牛云的工具类进行上传 参数为 文件的字节数组  和 文件名
            QiniuUtils.upload2Qiniu(imgFile.getBytes(),fileName);
            //保存到七牛云以后 将文件名保存到 对应的redis的set集合中
            jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_RESOURCES,fileName);
            //返回结果
            return new Result(true,MessageConstant.PIC_UPLOAD_SUCCESS,fileName);
        } catch (IOException e) {
            e.printStackTrace();
            //出错的话 就返回没有上传成功
            return  new Result(false,MessageConstant.PIC_UPLOAD_FAIL);
        }
    }


    @PreAuthorize("hasAuthority('SETMEAL_ADD')")
    @RequestMapping("/add")
    public  Result addSetmeal(@RequestBody setmeal_ids setmeal_ids){
        try {
            setmealService.addSetmeal(setmeal_ids);
            //保存好了说明图片的名字 放到数据库中  ,把保存到数据库中的img名字放到对应的redis中
            jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_DB_RESOURCES,setmeal_ids.getFormData().getImg());
            return  new Result(true,MessageConstant.ADD_SETMEAL_SUCCESS);
        } catch (RuntimeException e) {
            return  new Result(false,e.getMessage());
        }
    }

    @PreAuthorize("hasAuthority('SETMEAL_QUERY')")
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
      PageResult pageResult=  setmealService.findPage(currentPage,pageSize,queryString);
        return pageResult;
    }


    @PreAuthorize("hasAuthority('SETMEAL_DELETE')")
    @RequestMapping("/delete")
    public  Result delete(@RequestParam("id") Integer setmealId){
       Result result= setmealService.delete(setmealId);
        return  result;
    }
}
